// 命名空间
var webGame = webGame || {};

// 所有代码
webGame.path = 'modules/main/webGame/';
webGame.srcPath = webGame.path + 'src/';
webGame.src = [
    webGame.srcPath + 'webGameBase64.js'
];
// 所有资源
webGame.resPath = webGame.path + 'res/';
webGame.skeletonsPath = webGame.resPath + 'skeletons/';
webGame.res = {
    wait: webGame.resPath + 'wait.jpg',

    sp_chuanqi_json: webGame.skeletonsPath + 'chuanqi/chuanqi.json',
    sp_chuanqi_atlas: webGame.skeletonsPath + 'chuanqi/chuanqi.atlas',
    sp_chuanqi_png: webGame.skeletonsPath + 'chuanqi/chuanqi.png',

    sp_jiangshan2_json: webGame.skeletonsPath + 'jiangshan2/jiangshan2.json',
    sp_jiangshan2_atlas: webGame.skeletonsPath + 'jiangshan2/jiangshan2.atlas',
    sp_jiangshan2_png: webGame.skeletonsPath + 'jiangshan2/jiangshan2.png',

    // sp_rexuechuanqi_json: webGame.skeletonsPath + 'rexuechuanqi/rexuebaye.json',
    // sp_rexuechuanqi_atlas: webGame.skeletonsPath + 'rexuechuanqi/rexuebaye.atlas',
    // sp_rexuechuanqi_png: webGame.skeletonsPath + 'rexuechuanqi/rexuebaye.png',
};

webGame.gameType = {
    jsmr: 'jsmr',
    lcby: 'lcby',
    sghj: 'sghj',
    rxby:  'rxby'
};
webGame.h5id = {
    jsmr: '15524',
    lcby: '15524',
    sghj: '15524'
};
webGame.areacode = 'shannxi_mj';
webGame.scheme = app.shareMWID;
/**
 * 江山美人配置
 */
webGame.jsmr = {
    indexUrl: 'http://pay.yayayouxi.com/gt_cps/?r_plat=h5yaya-17018010&r_bid=17018000&r_host=gd-cpsguonei-sdk.raygame3.com&player=',
    h5id: webGame.h5id.jsmr,
    signKey: 'bdfbc61e923418aaf041b3e14ac8a7f7'
};

/**
 * 龙城霸业配置
 */
webGame.lcby = {
    indexUrl: 'http://pay.yayayouxi.com/xdpt/feiyu/index/2093/200048?&player=',
    h5id: webGame.h5id.lcby,
    signKey: 'ad2143145034b520c552b4be3a9d9de3'
};
/**
 * 热血霸业
 */

webGame.rxby = {
    indexUrl: 'https://pay.yayayouxi.com/rxby?player=',
    h5id: webGame.h5id.rxby,
    signKey: 'eb6a505ab10f1b0468bef3ebb97db24d',
};
/**
 * 时光幻境
 */
webGame.sghj = {
    indexUrl: 'http://pay.yayayouxi.com/sghj/?player=',
    h5id: webGame.h5id.sghj,
    signKey: 'e373cb32f456a88e01b74d162a625e14',
    isBrowser: false
};


/**
 * 开启江山美人
 * @param scene
 * @param end_url
 */
webGame.openjsmr = function (scene, end_url) {
    var webView = new ccui.WebView(end_url);
    scene.addChild(webView, 1, 'webview');
    webView.setContentSize(cc.winSize.width, cc.winSize.height);
    webView.setPosition(cc.winSize.width / 2, cc.winSize.height / 2);
    webView.setJavascriptInterfaceScheme(webGame.scheme);
    if (cc.sys.isNative) {
        webView.setOnJSCallback(function (sender, url) {
            console.log('url ===:' + url);
            resumeMusic();
            webView.removeFromParent(true);
            showBoard('HallBoard');
        });
    }
};

/**
 * 开启龙图霸业
 * @param scene
 * @param end_url
 */
webGame.openlcby = function (scene, end_url) {
    var waitbg = new cc.Sprite(webGame.res.wait);
    waitbg.setPosition(cc.winSize.width / 2, cc.winSize.height / 2);
    waitbg.setFlippedX(true);
    waitbg.setFlippedY(true);
    scene.addChild(waitbg);
    var webView = new ccui.WebView(end_url);
    scene.addChild(webView, 1, 'webview');
    webView.setContentSize(cc.winSize.width, cc.winSize.height);
    webView.setPosition(cc.winSize.width / 2, cc.winSize.height / 2);
    webView.setJavascriptInterfaceScheme(webGame.scheme);
    if (cc.sys.isNative) {
        webView.setOnJSCallback(function (sender, url) {
            console.log('url ===:' + url);
            resumeMusic();
            webView.removeFromParent(true);
            showBoard('HallBoard');
        });
    }
};

/**
 * 开启热血霸业
 * @param scene
 * @param end_url
 */
webGame.openrxby = function (scene, end_url) {
    var waitbg = new cc.Sprite(webGame.res.wait);
    waitbg.setPosition(cc.winSize.width / 2, cc.winSize.height / 2);
    scene.addChild(waitbg);
    var webView = new ccui.WebView(end_url+'&#http://');
    scene.addChild(webView, 1, 'webview');
    webView.setContentSize(cc.winSize.width, cc.winSize.height);
    webView.setPosition(cc.winSize.width / 2, cc.winSize.height / 2);
    webView.setJavascriptInterfaceScheme(webGame.scheme);
    if (cc.sys.isNative) {
        webView.setOnJSCallback(function (sender, url) {
            console.log('url ===:' + url);
            resumeMusic();
            webView.removeFromParent(true);
            showBoard('HallBoard');
        });
    }
};


/**
 * 开启时光幻境
 * @param scene
 * @param end_url
 */
webGame.opensghj = function (scene, end_url) {
    var webView = new ccui.WebView(end_url);
    scene.addChild(webView, 1, 'webview');
    webView.setContentSize(cc.winSize.width, cc.winSize.height);
    webView.setPosition(cc.winSize.width / 2, cc.winSize.height / 2);
    webView.setJavascriptInterfaceScheme(webGame.scheme);
    if (cc.sys.isNative) {
        webView.setOnJSCallback(function (sender, url) {
            console.log('url ===:' + url);
            resumeMusic();
            webView.removeFromParent(true);
            showBoard('HallBoard');
        });
    }
    // var waitbg = new cc.Sprite(webGame.res.wait);
    // waitbg.setPosition(cc.winSize.width / 2, cc.winSize.height / 2);
    // waitbg.setFlippedX(true);
    // waitbg.setFlippedY(true);
    // scene.addChild(waitbg);
    // var webView = new ccui.WebView(end_url);
    // scene.addChild(webView, 1, 'webview');
    // webView.setContentSize(cc.winSize.width, cc.winSize.height);
    // webView.setPosition(cc.winSize.width / 2, cc.winSize.height / 2);
    // webView.setJavascriptInterfaceScheme(webGame.scheme);
    // if (cc.sys.isNative) {
    //     webView.setOnJSCallback(function (sender, url) {
    //         console.log('url ===:' + url);
    //         resumeMusic();
    //         webView.removeFromParent(true);
    //         showBoard('HallBoard');
    //     });
    // }
};
/**
 * areacode 区id 找陈世鑫对
 * scheme 跳回app的head,默认每个包的魔窗
 * h5id 小雷哥那边用该字段来区分游戏 找陈世鑫
 */
webGame.openGameWebView = function (gameType) {
    var webGameData = webGame[gameType];
    if (!webGameData) {
        console.log('外链游戏参数错误，请核对外链游戏名称是否正确!!!');
        return;
    }
    var indexUrl = webGameData.indexUrl;
    var player = {
        'playerId': gameData.uid,
        'areacode': webGame.areacode
    };
    var base64_player = webGameBase64.encode(JSON.stringify(player));
    var timestamp = getCurTimestamp();
    var nonce = Math.ceil(100000 + Math.random() * 899999);
    var signKey = webGameData.signKey;
    var md5 = Crypto.MD5(base64_player + timestamp + nonce + signKey);
    var end_url = indexUrl + base64_player + '&h5id=' + webGameData.h5id + '&timestamp=' + timestamp + '&nonce=' + nonce + '&sign=' + md5;

    console.log('end_url===：' + end_url);
    stopMusic();
    var newScene = new cc.Scene();
    switch (gameType) {
        case webGame.gameType.jsmr:
            webGame.openjsmr(newScene, end_url);
            break;
        case webGame.gameType.lcby:
            webGame.openlcby(newScene, end_url);
            break;
        case webGame.gameType.rxby:
            webGame.openrxby(newScene, end_url);
            break;
        case webGame.gameType.sghj:
            webGame.opensghj(newScene, end_url);
            return;
    }
    cc.director.runScene(new cc.TransitionFade(0.01, newScene));
};